﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LX.Core.Utils;
using NPOI.SS.Formula.Functions;
using S4Store.Service;
using S4Store.Domain;
using S4Store.Models.ImproveGuide;

namespace S4Store.Controllers
{
    public class ImproveGuideController : ReportBaseController
    {
        public ImproveGuideService ImproveGuideService { get; set; }
        public EmployeeService SellerService { get; set; }

        public ActionResult AddImproveGuide(int guidetype, string referurl, string referdesc)
        {
            ImproveGuide model = new ImproveGuide();
            model.Type = guidetype;
            model.StoreId = Store.Id;
            model.ReferenceUrl = referurl;
            model.ReferenceDesc = referdesc;
            using (var t = new Transtion())
            {
                t.Inject(SellerService);
                return View(model);
            }
        }

        public ActionResult Edit(int id = 0)
        {
            using (var t = new Transtion())
            {
                t.Inject(ImproveGuideService);

                var querysource = ImproveGuideService.GetQuery();
                var model = querysource.FirstOrDefault(x => x.Id == id);

                return View(model);
            }
        }

        [HttpPost]
        public JsonResult Edit(ImproveGuide model)
        {
            using (var t = new Transtion())
            {
                t.Inject(ImproveGuideService);

                var querysource = ImproveGuideService.GetQuery();
                var editmodel = querysource.FirstOrDefault(x => x.Id == model.Id);
                editmodel.ImproveContent = model.ImproveContent;
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success = true
                    }
                };
            }
        }

        [HttpPost]
        public JsonResult Add(ImproveGuide model)
        {
            using (var t = new Transtion())
            {
                t.Inject(ImproveGuideService);
                model.ImproveUserId = CurrentUser.Id;
                //model.StatusValue = (int)Domain.ImproveGuide.Status.Draft;
                ImproveGuideService.AddImproveGuide(model);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success = true
                    }
                };
            }
        }

        public ActionResult Index()
        {
                return View();            
        }

        public ActionResult AdminIndex()
        {
            IndexModel im = new IndexModel();
            using (var t = new Transtion())
            {
                t.Inject(StoreService);
                im.Stores = StoreService.GetQuery().ToList();
            }
            return View(im);
        }

        public ActionResult AdminQuery()
        {
            QueryModel query = new QueryModel
            {
                Page = 1,
                PageSize = 20
            };
            return AdminQuery(query);
        }

        [HttpPost]
        public ActionResult AdminQuery(QueryModel query)
        {
            using (var t = new Transtion())
            {
                t.Inject(ImproveGuideService);

                var querysource = ImproveGuideService.GetQuery();
                if (CurrentUser.Position == Position.SystemManager)
                {
                    if (query.Store > 0)
                        querysource = querysource.Where(x => x.StoreId == query.Store);
                }
                else
                    querysource = querysource.Where(x => x.StoreId == CurrentUser.StoreId.Value);
                //if (query.Store > 0)
                //{
                //    querysource = querysource.Where(o => o.StoreId == query.Store);
                //}

                int total = querysource.Count();
                querysource = querysource.OrderByDescending(x => x.Id);
                if (query.Page > 1 && query.PageSize > 0)
                {
                    querysource = querysource.Skip((query.Page - 1) * query.PageSize);
                }
                querysource = querysource.Take(query.PageSize);
                LX.Core.Query.PagedList<ImproveGuide> data = new LX.Core.Query.PagedList<ImproveGuide>
                {
                    Data = querysource.ToList(),
                    Page = query.Page,
                    PageSize = query.PageSize,
                    Total = total

                };
                return View(data);
            }
        }


        public ActionResult Query()
        {
            QueryModel query = new QueryModel
            {
                Page = 1,
                PageSize = 20
            };
            if (CurrentUser.Position == Position.SalesManager)
            {
                query.type = ImproveGuide.ImproveGuideType.Sale.ToInt();
            }
            else if (CurrentUser.Position == Position.MarketingManager)
            {
                query.type = ImproveGuide.ImproveGuideType.Markting.ToInt();
            }
            else if (CurrentUser.Position == Position.AftersalesManager)
            {
                query.type = ImproveGuide.ImproveGuideType.AfterSale.ToInt();
            }
            return Query(query);
        }

        [HttpPost]
        public ActionResult Query(QueryModel query)
        {
            using (var t = new Transtion())
            {
                t.Inject(ImproveGuideService);

                var querysource = ImproveGuideService.GetQuery();
                if (query.type == 0)
                {
                    querysource = querysource.Where(x => x.StoreId == CurrentUser.StoreId.Value && x.StatusValue == (int)S4Store.Domain.ImproveGuide.Status.Official);
                }else
                 querysource = querysource.Where(x => x.Type==query.type&&x.StoreId==CurrentUser.StoreId.Value && x.StatusValue==(int)S4Store.Domain.ImproveGuide.Status.Official);
               
                int total = querysource.Count();
                querysource = querysource.OrderByDescending(x => x.Id);
                if (query.Page > 1 && query.PageSize > 0)
                {
                    querysource = querysource.Skip((query.Page - 1) * query.PageSize);
                }
                querysource = querysource.Take(query.PageSize);
                LX.Core.Query.PagedList<ImproveGuide> data = new LX.Core.Query.PagedList<ImproveGuide>
                {
                    Data = querysource.ToList(),
                    Page = query.Page,
                    PageSize = query.PageSize,
                    Total = total

                };
                return View(data);
            }
        }

        public ActionResult Detail(int id){
            using (var t = new Transtion())
            {
                t.Inject(ImproveGuideService);

                var querysource = ImproveGuideService.GetQuery();
                var model = querysource.FirstOrDefault(x => x.Id == id);
                if (CurrentUser.Position == Position.SalesManager 
                    || CurrentUser.Position == Position.MarketingManager 
                    || CurrentUser.Position == Position.AftersalesManager)
                {
                    model.IsRead = true;
                }
                if (CurrentUser.Position == Position.StoreManager)
                {
                    model.IsManagerRead = true;
                }

                t.Commit();
                return View(model);
            }
        }

        [HttpGet]
        public JsonResult Send(int id = 0)
        {
            using (var t = new Transtion())
            {
                t.Inject(ImproveGuideService);
                var querysource = ImproveGuideService.GetQuery();
                var model = querysource.FirstOrDefault(x => x.Id == id);
                model.StatusValue = (int)Domain.ImproveGuide.Status.Official;
                t.Commit();
                return new JsonResult
                {
                    JsonRequestBehavior = System.Web.Mvc.JsonRequestBehavior.AllowGet,
                    Data = new
                    {
                        success = true,
                    }
                };
            }
        }


        [HttpPost]
        public JsonResult Delete(List<int> ids)
        {
            if (Helper.ArrayIsEmpty(ids))
            {
                throw new Exception("请选择要删除的项");
            }
            using (var t = new Transtion())
            {
                t.Inject<ImproveGuideService>(ImproveGuideService);
                foreach (var id in ids)
                {
                    ImproveGuideService.Delete(id);
                }
                t.Commit();
            }
            return new JsonResult
            {
                Data = new
                {
                    success = true,
                    result = "删除成功"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

    }
}
